package com.trex.modules.tbl.kclb.web;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sys.utils.UserUtils;
import com.trex.modules.tbl.kclb.entity.TblZdKclb;
import com.trex.modules.tbl.kclb.service.TblZdKclbService;

/**
 * 扣车类别Controller
 * @author jy
 * @version 2022-12-06
 */
@Controller
@RequestMapping(value = "/tbl/kclb/tblZdKclb")
public class TblZdKclbController extends BaseController {

	@Autowired
	private TblZdKclbService tblZdKclbService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TblZdKclb get(String recId, boolean isNewRecord) {
		return tblZdKclbService.get(recId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	//@RequiresPermissions("tbl:kclb:tblZdKclb:view")
	@RequestMapping(value = {"list", ""})
	public String list( @RequestParam(value = "userid", required = false) String userid,
						@RequestParam(value = "postid", required = false) String postid,TblZdKclb tblZdKclb, Model model) {
		if (userid != null) {
			// 将 userid 添加到模型中或者在逻辑中使用
			tblZdKclb.setUserid(userid);
		}
		if (postid != null) {
			// 将 postid 添加到模型中或者在逻辑中使用
			tblZdKclb.setPostid(postid);
		}
		model.addAttribute("tblZdKclb", tblZdKclb);
		return "modules/tbl/kclb/tblZdKclbList";
	}
	
	/**
	 * 查询列表数据
	 */
	//@RequiresPermissions("tbl:kclb:tblZdKclb:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TblZdKclb> listData(TblZdKclb tblZdKclb, HttpServletRequest request, HttpServletResponse response) {
		tblZdKclb.setPage(new Page<>(request, response));
		Page<TblZdKclb> page = tblZdKclbService.findPage(tblZdKclb);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	//@RequiresPermissions("tbl:kclb:tblZdKclb:view")
	@RequestMapping(value = "form")
	public String form(TblZdKclb tblZdKclb, Model model) {
		model.addAttribute("tblZdKclb", tblZdKclb);
		return "modules/tbl/kclb/tblZdKclbForm";
	}

	/**
	 * 保存数据
	 */
	//@RequiresPermissions("tbl:kclb:tblZdKclb:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TblZdKclb tblZdKclb, HttpServletRequest request) {
		String I_IP_ADDR = request.getRemoteHost(); // 取IP
		String I_USERCODE = UserUtils.getUser().getUserCode(); // 取用户id
		String I_USERNAME = UserUtils.getUser().getUserName(); //用户名
		
		tblZdKclb.setIp(I_IP_ADDR);
		tblZdKclb.setUserCode(I_USERCODE);
		tblZdKclb.setUserName(I_USERNAME);
		tblZdKclb.setUpdTime(new Date());
		
		TblZdKclb tblZdKclb_new = new TblZdKclb();
		tblZdKclb_new.setKclbCode(tblZdKclb.getKclbCode());
		
		if(tblZdKclb.getIsNewRecord()) {
			//新增 判断记录是否已存在
			if(tblZdKclbService.findCount(tblZdKclb_new) > 0) {
				return renderResult(Global.FALSE, text("扣车类别编码已存在，无法新增！"));
			};
		}else {
			//修改
			List<TblZdKclb> listU = tblZdKclbService.findList(tblZdKclb_new);

			for(int a=0;a<listU.size();a++) {
				TblZdKclb tblZdKclb_new2 = listU.get(a);
				if(tblZdKclb_new2.getRecId().equals(tblZdKclb.getRecId())) {
					continue;
				}else {
					return renderResult(Global.FALSE, text("扣车类别编码已存在，修改失败！"));
				}
			}
		}
		tblZdKclbService.save(tblZdKclb);
		return renderResult(Global.TRUE, text("保存扣车类别成功！"));
	}
	
	/**
	 * 删除数据
	 */
	//@RequiresPermissions("tbl:kclb:tblZdKclb:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TblZdKclb tblZdKclb) {
		tblZdKclbService.delete(tblZdKclb);
		return renderResult(Global.TRUE, text("删除扣车类别成功！"));
	}
	
}